import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '仪表盘', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: '权限管理',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: 'Page Permission',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive'),
        name: 'DirectivePermission',
        meta: {
          title: 'Directive Permission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: 'RolePermission',
        meta: {
          title: 'Role Permission',
          roles: ['admin']
        }
      }
    ]
  },

  {
    path: '/product',
    component: Layout,
    redirect: '/product',
    meta: { title: '商品管理', icon: 'el-icon-goods', affix: true },
    children: [
      {
        path: 'product-list',
        component: () => import('@/views/product/index'),
        name: 'ProductList',
        meta: { title: '商品管理', affix: true }
      },
      {
        path: 'product-category',
        component: () => import('@/views/product/product-category'),
        name: 'ProductCategory',
        meta: { title: '分类管理', affix: true }
      },
      {
        path: 'product-specification',
        component: () => import('@/views/product/product-specification'),
        name: 'ProductSpecification',
        meta: { title: '规格管理', affix: true }
      }
    ]
  },

  {
    path: '/users',
    component: Layout,
    children: [
      {
        path: 'users',
        component: () => import('@/views/users'),
        name: 'Suers',
        meta: { title: '用户管理', icon: 'el-icon-user', affix: true }
      }
    ]
  },

  {
    path: '/order',
    component: Layout,
    children: [
      {
        path: 'order-list',
        component: () => import('@/views/order/list'),
        name: 'OrderList',
        meta: {
          title: '订单管理', icon: 'el-icon-receiving', affix: true
        }
      }
    ]
  },

  {
    path: '/image',
    component: Layout,
    children: [
      {
        path: 'image',
        component: () => import('@/views/image'),
        name: 'Image',
        meta: { title: '图库管理', icon: 'el-icon-picture', affix: true }
      }
    ]
  },

  {
    path: '/advertising',
    component: Layout,
    children: [
      {
        path: 'advertising-list',
        component: () => import('@/views/advertising/index'),
        name: 'Advertising',
        meta: {
          title: '广告位管理', icon: 'el-icon-data-analysis', affix: true
        }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
